package com.czl.module_storehouse.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.czl.base.data.bean.tengyun.ReceiveBean;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.fragment.BaseRecyclerViewFragment;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_service.event.ReceiveHomeEvent;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.receive.add.ReceiveAddActivity;
import com.czl.module_storehouse.activity.receive.details.ReceiveDetailsActivity;
import com.czl.module_storehouse.activity.receive.details.ReceiveDetailsPostActivity;
import com.czl.module_storehouse.adapter.ReceiveHomeAdapter;
import com.czl.module_storehouse.databinding.CommonButtonBinding;
import com.czl.module_storehouse.databinding.FragmentReceiveBinding;
import com.czl.module_storehouse.event.ReceiveEvent;
import com.czl.module_storehouse.mvp.presenter.ReceiveHomePresenter;
import com.czl.module_storehouse.mvp.view.ReceiveHomeView;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.greenrobot.eventbus.EventBus;

/**
 * @author by xubin,
 * @date on 2022/1/25.
 * PS: Not easy to write code, please indicate.
 */
public class ReceiveFragment extends BaseRecyclerViewFragment<FragmentReceiveBinding> implements ReceiveHomeView {

    @InjectPresenter
    ReceiveHomePresenter mPresenter;

    private ReceiveHomeAdapter mAdapter;
    private int mState;
    private boolean loading;

    public static ReceiveFragment newInstance(int type) {
        ReceiveFragment fragment = new ReceiveFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("state", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected FragmentReceiveBinding getBinding(LayoutInflater layoutInflater, ViewGroup container) {
        return null;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mState = getArguments() != null ? getArguments().getInt("state") : 0;
        if (mState == 0) {
            CommonButtonBinding buttonBinding = CommonButtonBinding.inflate(getLayoutInflater(), mRecyclerViewBinding.containerBottom, true);
            buttonBinding.button.setText("添加领用单");
            buttonBinding.button.setOnClickListener(v -> startActivity(new Intent(requireContext(), ReceiveAddActivity.class)));
        }
        mAdapter = new ReceiveHomeAdapter(R.layout.item_receive);
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            ReceiveBean item = mAdapter.getItem(position);
            if (R.id.tv_receive_total == view.getId()) {
                getDetailsActivity(item);
            } else if (R.id.cl_root == view.getId()) {
                goInfoActivity(item);
            }
        });
        mRecyclerViewBinding.recyclerView.setAdapter(mAdapter);

        LiveEventBus.get(ReceiveHomeEvent.class).observe(this, event -> {
            if (event != null && !loading) {
                reload();
            }
        });
    }

    private void getDetailsActivity(ReceiveBean bean) {
        ReceiveEvent event = new ReceiveEvent();
        event.setReceiveBean(bean);
        EventBus.getDefault().postSticky(event);
        Intent intent = new Intent(requireContext(), ReceiveDetailsActivity.class);
        startActivity(intent);
    }

    private void goInfoActivity(ReceiveBean bean) {
        Intent intent = new Intent();
        ReceiveEvent event = new ReceiveEvent();
        event.setReceiveBean(bean);
        EventBus.getDefault().postSticky(event);
        if (mState == 1) {
            intent.setClass(requireContext(), ReceiveDetailsActivity.class);
        } else {
            intent.setClass(requireContext(), ReceiveDetailsPostActivity.class);
        }
        startActivity(intent);
    }

    @Override
    protected void reload() {
        super.reload();
        loading = true;
        loadData(true);
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        loadData(false);
    }

    private void loadData(boolean reload) {
        if (mState == 0) {
            mPresenter.getNeedDealtReceiveList(reload);
        } else {
            mPresenter.getDealtReceiveList(reload);
        }
    }

    @Override
    public void hideDialogLoading() {
        super.hideDialogLoading();
        loading = false;
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        if (t instanceof ListBean) {
            handleResponseData(mAdapter, mPresenter.getPageNo(), (ListBean<ReceiveBean>) t);
        }
    }
}
